package com.car.carcontrol.base;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;


import com.car.carcontrol.util.ToastUtils;


/**
 * 自定义Application
 * 
 * @author wutao
 *
 */
public class BaseApplication extends Application {
	public  static Context context;
	private static BaseApplication app;
	private String packageName = "";
	private PackageInfo packageInfo;
//	private IBLE bleManager;
	private boolean debug = false;
	private boolean test = false;
	private boolean testLog = false;
	public static int width;
	public static int height;
	public static float density;

	public static  BaseApplication getInstance() {
		return app;
	}

	@Override
	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public PackageInfo getPackageInfo() {
		return packageInfo;
	}

	public void setPackageInfo(PackageInfo packageInfo) {
		this.packageInfo = packageInfo;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}

	public boolean isTestLog() {
		return testLog;
	}

	public void setTestLog(boolean testLog) {
		this.testLog = testLog;
	}

	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
//		MultiDex.install(this);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		app = this;

		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(metrics);
		density = metrics.density;
		width = metrics.widthPixels;
		height = metrics.heightPixels;

		Log.e("BaseApplication===", density+"==="+width+"==="+height);

//		BluetoothContext.set(this);

		setDebug(false);
//		setTestLog(true);
		setTestLog(false);
//		setTest(true);
		setTest(false);

//		registerReceiver(broadcastReceiver2, Config.initFilter());
//		GlobalParameterUtils.getInstance().setLockType(LockType.MTS);

//		AdHub.initialize(this, "2597");

//		bleManager = new AndroidBle(this);
//		RxTool.init(getApplicationContext());
//		ToastUtils.init(getApplicationContext());
//		// registerUncaughtExceptionHandler();
//		initImageLoader(getApplicationContext());
		try {
			packageName = getPackageName();
			packageInfo = getPackageManager().getPackageInfo(packageName, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		JPushInterface.setDebugMode(false); // 设置开启日志,发布时请关闭日志
//		JPushInterface.init(this); // 初始化 JPush
//
//		if (SharedPreferencesUrls.getInstance().getInt("versionCode", 0) != packageInfo.versionCode) {
//			SharedPreferencesUrls.getInstance().putBoolean("isFirst", true);
//			SharedPreferencesUrls.getInstance().putInt("versionCode", packageInfo.versionCode);
//		} else {
//			SharedPreferencesUrls.getInstance().putBoolean("isFirst", false);
//		}
		ToastUtils.init(getApplicationContext());



	}


	// 各个平台的配置，建议放在全局Application或者程序入口
	{
		// 微信 wx12342956d1cab4f9,a5ae111de7d9ea137e88a5e02c07c94d
//		PlatformConfig.setWeixin("wx86d98ec252f67d07", "4e4aafa841609025036b0367d30ef052");
//		// QQ
//		PlatformConfig.setQQZone("1105975305", "wkfWoPW8aYGwdvcL");
	}

//	private static void initImageLoader(Context context) {
//
//		File cacheDir = StorageUtils.getCacheDirectory(context);
//
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
//				.memoryCacheExtraOptions(480, 800) // maxwidth, max
//				// height，即保存的每个缓存文件的最大长宽
//				.threadPoolSize(3)// 线程池内加载的数量
//				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
//				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // 你可以通过自己的内存缓存实现
//				.memoryCacheSize(2 * 1024 * 1024).diskCacheSize(50 * 1024 * 1024)
//				.diskCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5加密
//				.tasksProcessingOrder(QueueProcessingType.LIFO).diskCacheFileCount(100) // 缓存的文件数量
//				.diskCache(new UnlimitedDiskCache(cacheDir))// 自定义缓存路径
//				.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
//				.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout(5s),readTimeout(30s)超时时间
//				.writeDebugLogs() // Remove for releaseapp
//				.build();// 开始构建
//
//		ImageLoader.getInstance().init(config);
//	}

	// 注册App异常崩溃处理器
	private void registerUncaughtExceptionHandler() {
		Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
	}

//	@Override
//	public void onLowMemory() {
//		super.onLowMemory();
//		System.gc();
//	}
//
//	public IBLE getIBLE() {
//		if (bleManager == null) {
//
//			Log.e("main===", "IBLE====");
//
//			bleManager = new AndroidBle(this);
//		}
//		return bleManager;
//	}

}
